import { createFileRoute } from '@tanstack/react-router'
import Rules from '@/features/rules'
import { ruleKeys, rules, rulesSelectTree, rulesTableTree } from '@/api/rules'
import { Rule } from '@/features/rules/data/schema'
import { ListItem, TreeItem } from '@/components/tree-select/schema'
export const Route = createFileRoute('/_authenticated/rules/')({
  component: Rules,
  loader: async ({ context: { queryClient } }): Promise<{ ruleTable: Rule[], ruleSelect: TreeItem[], ruleList: ListItem[] }> => {
    const ruleTableResponse = await queryClient.fetchQuery({
      queryKey: ruleKeys.rulesTableTree(),
      queryFn: rulesTableTree
    })
    const ruleSelectResponse = await queryClient.fetchQuery({
      queryKey: ruleKeys.rulesSelectTree(),
      queryFn: rulesSelectTree
    })
    const rulesResponse = await queryClient.fetchQuery({
      queryKey: ruleKeys.all(),
      queryFn: rules
    })
    return { ruleTable: ruleTableResponse.data.data, ruleSelect: ruleSelectResponse.data.data, ruleList: rulesResponse.data.data };
  }
})
